<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "set_param".
 *
 * @property int $id
 * @property float $vip1
 * @property float $vip2
 * @property int $vip3
 * @property float $vip4_price
 * @property int $vip4_count
 * @property float $vip5_price
 * @property int $vip5_count
 * @property float $v1_1 VIP推荐
 * @property float $v1_2
 * @property int $bl 伯乐百分比
 * @property float $jd1 见单奖
 * @property float $jd2
 * @property float $equal1 创始人平级
 * @property float $equal2
 * @property float $r_equal1 复购平级
 * @property float $r_equal2
 * @property int $profit 分红百分比
 * @property int $vip3_ratio 推荐比例,百分比
 * @property int $vip4_ratio
 * @property int $vip2_ratio
 * @property int $vip5_ratio
 * @property float $r1 22直推
 * @property float $r2
 */
class SetParam extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_param';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['vip1', 'vip2', 'vip4_price', 'vip5_price', 'v1_1', 'v1_2', 'jd1', 'jd2', 'equal1', 'equal2', 'r_equal1', 'r_equal2', 'r1', 'r2'], 'number'],
            [['vip3', 'vip4_count', 'vip5_count', 'bl', 'profit', 'vip2_ratio', 'vip3_ratio', 'vip4_ratio', 'vip5_ratio'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'vip1' => 'Vip 1',
            'vip2' => 'Vip 2',
            'vip3' => 'Vip 3',
            'vip4_price' => 'Vip 4 Price',
            'vip4_count' => 'Vip 4 Count',
            'vip5_price' => 'Vip 5 Price',
            'vip5_count' => 'Vip 5 Count',
            'v1_1' => 'V 1  1',
            'v1_2' => 'V 1  2',
            'bl' => 'Bl',
            'jd1' => 'Jd 1',
            'jd2' => 'Jd 2',
            'equal1' => 'Equal 1',
            'equal2' => 'Equal 2',
            'r_equal1' => 'R Equal 1',
            'r_equal2' => 'R Equal 2',
            'profit' => 'Profit',
            'vip3_ratio' => 'Vip 3 Ratio',
            'vip4_ratio' => 'Vip 4 Ratio',
            'vip5_ratio' => 'Vip 5 Ratio',
            'r1' => 'R 1',
            'r2' => 'R 2',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetParam' => $data]) && $model->save()) {
            return true;
        }
    }
}
